package api

import (
	"fmt"
	"time"
	"vite-tsc-gin/pkg/utils"
)

type User struct {
	Username string `json:"username" gorm:"column:username" validate:"required"`
	Status   int    `json:"status" gorm:"column:status" validate:"omitempty"`
	// Required: true
	Nickname string `json:"nickname" gorm:"column:nickname" validate:"omitempty"`
	// Required: true
	Password string `json:"password,omitempty" gorm:"column:password" validate:"required"`
	// Required: true
	Email     string    `json:"email" gorm:"column:email" validate:"required,email,min=1,max=100"`
	Phone     string    `json:"phone" gorm:"column:phone" validate:"omitempty"`
	IsAdmin   int       `json:"isAdmin,omitempty" gorm:"column:isAdmin" validate:"omitempty"`
	LoginedAt time.Time `json:"loginedAt,omitempty" gorm:"column:loginedAt"`
}

func (u *User) Compare(pwd string) error {
	if err := utils.Compare(u.Password, pwd); err != nil {
		return fmt.Errorf("failed to compile password: %w", err)
	}
	return nil
}
